﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class TiebaController : Controller
    {
        //
        // GET: /Tieba/

        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords= "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
          
            var qry = GswTblTieba.GetTiebaInfo(sdate, edate, keywords, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }

        public ActionResult TiebaView(int id = 0, string m = "", string f = "", string p = "", string fid = "",
            string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswViewTieba.SingleOrDefault(id);
            qry.TiebaReplies = GswViewTiebaReply.Fetch("where MasterId=" + id + " order by ReplyDate desc");
            return View(qry);
        }
        public ActionResult ReplyDelete(int id = 0,int rid=0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblTieba.Delete(id);
            GswTblTiebaReply.Delete(rid);
            var qry = GswViewTieba.SingleOrDefault(id);
            qry.TiebaReplies = GswViewTiebaReply.Fetch("where MasterId=" + id + " order by ReplyDate desc");
            return View("TiebaView",qry);
        }

        public ActionResult Delete(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblTieba.Delete(id);
            
            var qry = GswTblTieba.GetTiebaInfo(sdate, edate, keywords, GswTblUser.CurrentUser.CustomerId);
            return View("Index",qry);
        }
    }
}
